﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

using SBPweb.Forge.Controls.Silverlight.Utilities;

namespace SBPweb.Forge.Controls.Silverlight
{

    public partial class ToolBarItem : UserControl
    {

        #region Constructor logic

        public ToolBarItem()
        {
            this.IsEnabledChanged += new DependencyPropertyChangedEventHandler(ToolBarItem_IsEnabledChanged);
            InitializeComponent();
        }

        #endregion

        #region Property accessors

        public Stretch Stretch
        {
            get
            {
                return image.Stretch;
            }
            set
            {
                image.Stretch = value;
            }
        }

        public ImageSource ImageSource
        {
            get
            {
                return image.Source;
            }
            set
            {
                image.Source = value;
            }
        }

        public string Caption
        {
            get
            {
                return caption.Text;
            }
            set
            {
                caption.Text = value;
            }
        }

        public bool IsCaptionVisible
        {
            get
            {
                return caption.Visibility == Visibility.Visible;
            }
            set
            {
                if (value)
                {
                    caption.Visibility = Visibility.Visible;
                }
                else
                {
                    caption.Visibility = Visibility.Collapsed;
                }
            }
        }

        public double? OpacityNormal
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnter).From;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnter).From = value;
                GetAnimationByStoryboard(storyMouseLeave).To = value;
                GetAnimationByStoryboard(storyEnableChanged).To = value;
                GetAnimationByStoryboard(storyEnableChangedDisabled).From = value;
            }
        }

        public double? OpacityDisabled
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnterDisabled).From;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnterDisabled).From = value;
                GetAnimationByStoryboard(storyMouseLeaveDisabled).To = value;
                GetAnimationByStoryboard(storyEnableChanged).From = value;
                GetAnimationByStoryboard(storyEnableChangedDisabled).To = value;
            }
        }

        public double? OpacityHover
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnter).To;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnter).To = value;
                GetAnimationByStoryboard(storyMouseLeave).From = value;
            }
        }

        public double? OpacityHoverDisabled
        {
            get
            {
                return GetAnimationByStoryboard(storyMouseEnterDisabled).To;
            }
            set
            {
                GetAnimationByStoryboard(storyMouseEnterDisabled).To = value;
                GetAnimationByStoryboard(storyMouseLeaveDisabled).From = value;
            }
        }

        #endregion

        #region Event handlers

        private void Element_MouseEnter(object sender, MouseEventArgs e)
        {
            AnimationHelper.StartStory(this, sender as FrameworkElement, "storyMouseEnter");
        }

        private void Element_MouseLeave(object sender, MouseEventArgs e)
        {
            AnimationHelper.StartStory(this, sender as FrameworkElement, "storyMouseLeave");
        }

        private void ToolBarItem_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            AnimationHelper.StartStory(this, sender as FrameworkElement, "storyEnableChanged");
        }

        #endregion

        private DoubleAnimation GetAnimationByStoryboard(Storyboard sb)
        {
            return sb.Children[0] as DoubleAnimation;
        }

    }

}